package com.yurun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yurun.entity.Business;
import com.yurun.exception.PersistenceException;
import com.yurun.mapper.BusinessMapper;
import com.yurun.model.vo.PageResult;
import com.yurun.model.vo.article.ArticleInfo;
import com.yurun.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusinessServieImpl implements BusinessService {
    @Autowired
    BusinessMapper businessMapper;

    // 每页显示10条
    private static final int pageSize=10;
    // 文章排序方式
    private static final String orderBy="is_top desc, create_time desc";

    @Override
    public void addBusiness(Business business){
        if(businessMapper.addBusiness(business)!=1){
            throw new PersistenceException("添加业务失败");
        }
    }

    @Override
    public void deleteBusinessById(Long id){
        if(businessMapper.deleteBusinessById(id)!=1){
            throw new PersistenceException("删除业务失败");
        }
    }

    @Override
    public void updateBusiness(Business business){
        if(businessMapper.updateBusiness(business)!=1){
            throw new PersistenceException("更新业务失败");
        }
    }

    @Override
    public PageResult<Business> getBusinessList(Integer pageNum){
        PageHelper.startPage(pageNum,pageSize,orderBy);
        List<Business> businesses=businessMapper.getBusinessList();
        PageInfo<Business> pageInfo=new PageInfo<>(businesses);
        PageResult<Business> pageResult=new PageResult<>(pageInfo.getPages(),
                pageInfo.getList());
        return pageResult;
    }

    @Override
    public PageResult<Business> getBusinessListByCategoryId(Integer bcategoryId,Integer pageNum){
        PageHelper.startPage(pageNum,pageSize,orderBy);
        List<Business> businesses=businessMapper.getBusinessListByCategoryId(bcategoryId);
        PageInfo<Business> pageInfo=new PageInfo<>(businesses);
        PageResult<Business> pageResult=new PageResult<>(pageInfo.getPages(),
                pageInfo.getList());
        return pageResult;
    }

    @Override
    public Business getBusinessByName(String name){
        return businessMapper.getBusinessByName(name);
    }
}
